import { defineStore } from "pinia";
import { Local, Session } from "/@/utils/storage";
import router from "/@/router/index";

/**
 * 用户信息
 * @methods getUserInfo 设置用户信息
 */
export const useUserInfo = defineStore("userInfo", {
	state: (): UserInfoState => ({
		userInfo: {
			userName: "",
			photo: "",
			time: 0,
			roles: [],
			authBtnList: [],
		},
	}),
	actions: {
		// 获取用户信息
		async getUserInfo() {
			if (Local.has("token") && Local.has("userInfo")) {
				this.userInfo = Local.get("userInfo");
				return this.userInfo;
			} else {
				Session.clear();
				Local.remove("token");
				Local.remove("refresh");
				Local.remove("userInfo");
				router.replace({ path: "/login" });
				return Promise.reject("用户信息缓存失效，请重新登录");
			}
		},
	},
});
